package org.msfj.common.model;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @Description: element-ui 级联结果封装对象
 * @Author: like
 * @Date: 2017-06-22 15:36
 * @Version: 1.0
 * @Created in idea by autoCode
 */
public class CascaderResult implements Serializable {

    private static Logger logger = LoggerFactory.getLogger(CascaderResult.class);
    private String value;
    private String label;
    private boolean disabled = false;
    private List<CascaderResult> children;
    private String treePosition;

    public void setTreePosition(String treePosition) {
        this.treePosition = treePosition;
    }

    public String getTreePosition() {
        return treePosition;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public boolean isDisabled() {
        return disabled;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    public List<CascaderResult> getChildren() {
        return children;
    }

    public void setChildren(List<CascaderResult> children) {
        this.children = children;
    }

    public static List<CascaderResult> getCascaderResult(String valuePropertyName, String labelPropertyName, String treePositionPropertyName, String disableId, List list) {
        List<CascaderResult> cascaderResults = CollectionUtil.newArrayList();
        try {
            if (CollectionUtil.isNotEmpty(list)) {
                for (Object object : list) {
                    CascaderResult cascaderResult = new CascaderResult();
                    Method[] methods = object.getClass().getMethods();
                    List<CascaderResult> cascaderResultChildren;
                    for (Method method : methods) {
                        if (method.getName().startsWith("get") && method.getName().indexOf("get" + valuePropertyName) >= 0) {
                            String id = (String) method.invoke(object);
                            if (ObjectUtil.equal(id, disableId)) {
                                cascaderResult.setDisabled(true);
                            }
                            cascaderResult.setValue(StrUtil.utf8Str(id));
                        }
                        if (method.getName().startsWith("get") && method.getName().indexOf("get" + labelPropertyName) >= 0) {
                            cascaderResult.setLabel(StrUtil.utf8Str(method.invoke(object)));
                        }
                        if (method.getName().startsWith("get") && method.getName().indexOf("get" + treePositionPropertyName) >= 0) {
                            cascaderResult.setTreePosition(StrUtil.utf8Str(method.invoke(object)));
                        }
                        if (method.getName().indexOf("getChildren") >= 0) {
                            List<Object> children = (List<Object>) method.invoke(object);
                            if (CollectionUtil.isNotEmpty(children)) {
                                cascaderResultChildren = getCascaderResult(valuePropertyName, labelPropertyName, treePositionPropertyName, disableId, children);
                                cascaderResult.setChildren(cascaderResultChildren);
                            }
                        }
                    }
                    cascaderResults.add(cascaderResult);
                }
            }
        } catch (Exception e) {
//            e.printStackTrace();
//            ExceptionUtil.getMessage(e);
            logger.error("转换级联结构失败",e);
        }
        return cascaderResults;
    }
}
